package com.misc.tunewsreader;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.AbstractAction;

public class Help extends Activity {

    private ViewFlipper vf;
    private float lastX;
    private final int PAGE_INDICATOR = 1000;
    private int pages;

    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.help);
	ActionBar bar = (ActionBar) findViewById(R.id.actionbar);
	bar.setTitle(this.getString(R.string.help));
	bar.addAction(new DoneAction(this, R.drawable.bar_accept));

	vf = (ViewFlipper) findViewById(R.id.help_flipper);
	vf.addView(generatePage("Main screen", "", R.drawable.help_mainscreen),
		0);
	vf.addView(
		generatePage("Addings Newsgroup",
			"Click on a group to subscribe/unsubscribe.",
			R.drawable.help_groups), 1);
	vf.addView(
		generatePage(
			"Newsgroup menu",
			"On main screen, long click on a newsgroup for more options.",
			R.drawable.help_group_context), 2);
	vf.addView(generatePage(
		"Newsgroup menu I",
		"<b>Mark as read/unread</b>: Mark all downloaded posts as read/unread. Posts that are not downloaded are not marked as read.<br/>\n<br/>\n"
			+ "<b>Reset group</b>: Delete all posts. The group is then as if it has never been entered before.<br/>\n<br/>\n"
			+ "<b>Unsubscribe group</b>: The same as unsubscribing in the group view.<br/>\n<br/>\n"
			+ "<b>Cleanup group</b>: You are asked how many posts you want to keep, all other posts are deleted.",
		-1));
	vf.addView(generatePage(
		"Threads",
		"When clicking on a newsgroup, a list of threads is displayed with additional informations.",
		R.drawable.help_threads));
	vf.addView(generatePage(
		"Thread View",
		"Clickin on a thread brings you to the thread view, with the first post displayed and the list of replies at the bottom.",
		R.drawable.help_post));
	vf.addView(generatePage(
		"Reply menu",
		"Long pressing on a reply brings up the menu for this reply, allowing to mark as read/unread, share and cancel the post.",
		R.drawable.help_post_context));
	vf.addView(generatePage(
		"Post details",
		"On post details page, long press on an info to copy it to the clipboard.",
		R.drawable.help_post_details));
	vf.addView(generatePage(
		"Write post",
		"When creating a new thread or posting a reply, you are taken to this screen.",
		R.drawable.help_write_post));
	vf.addView(generatePage("Congratulations!",
		"You now know the basic functionality of this app.",
		R.drawable.androids));

	pages = vf.getChildCount();
	LinearLayout help_page = (LinearLayout) findViewById(R.id.help_page);

	// generate page indicators
	for (int i = 0; i < pages; i++) {
	    ImageView indicator = new ImageView(this);
	    indicator.setImageResource(R.drawable.page_inactive);
	    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
		    LinearLayout.LayoutParams.WRAP_CONTENT,
		    LinearLayout.LayoutParams.WRAP_CONTENT);
	    lp.setMargins(5, 5, 5, 5);
	    indicator.setLayoutParams(lp);
	    final int id = PAGE_INDICATOR + i;
	    indicator.setId(id);
	    indicator.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
		    vf.setDisplayedChild(id - PAGE_INDICATOR);
		    markActivePage();
		}
	    });
	    help_page.addView(indicator);
	}

	// initHelpPages();

	markActivePage();
    }

    private LinearLayout generatePage(String title, String text, int image) {
	LinearLayout page = new LinearLayout(this);
	LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
		LinearLayout.LayoutParams.FILL_PARENT,
		LinearLayout.LayoutParams.WRAP_CONTENT);
	page.setOrientation(LinearLayout.VERTICAL);
	lp.setMargins(15, 5, 15, 5);
	page.setLayoutParams(lp);

	TextView tv_title = new TextView(this);
	tv_title.setLayoutParams(lp);
	tv_title.setText(Html.fromHtml("<b>" + title + "</b>"));
	tv_title.setTextSize(25);
	page.addView(tv_title);

	if (!text.equals("")) {
	    TextView tv_text = new TextView(this);
	    tv_text.setLayoutParams(lp);
	    tv_text.setText(Html.fromHtml(text));
	    page.addView(tv_text);
	}

	if (image != -1) {
	    ImageView iv_image = new ImageView(this);
	    iv_image.setLayoutParams(lp);
	    iv_image.setImageResource(image);
	    page.addView(iv_image);
	}

	return page;
    }

    private void markActivePage() {
	int active = vf.getDisplayedChild();
	for (int i = 0; i < pages; i++) {
	    ImageView indicator = (ImageView) findViewById(PAGE_INDICATOR + i);
	    indicator.setImageResource((i == active) ? R.drawable.page_active
		    : R.drawable.page_inactive);
	}
    }

    @Override
    public boolean onTouchEvent(MotionEvent touchevent) {
	switch (touchevent.getAction()) {
	case MotionEvent.ACTION_DOWN: {
	    lastX = touchevent.getX();
	    break;
	}
	case MotionEvent.ACTION_UP: {
	    float currentX = touchevent.getX();
	    if (lastX < currentX) {
		if (vf.getDisplayedChild() == 0)
		    break;
		vf.setInAnimation(this, R.anim.in_from_left);
		vf.setOutAnimation(this, R.anim.out_to_right);
		vf.showPrevious();
		markActivePage();
	    }
	    if (lastX > currentX) {
		if (vf.getDisplayedChild() == pages - 1)
		    break;
		vf.setInAnimation(this, R.anim.in_from_right);
		vf.setOutAnimation(this, R.anim.out_to_left);
		vf.showNext();
		markActivePage();
	    }
	    break;
	}
	}
	return false;
    }

    public static class DoneAction extends AbstractAction {
	private Context mContext;

	public DoneAction(Context context, int drawable) {
	    super(drawable);
	    mContext = context;
	}

	@Override
	public void performAction(View view) {
	    ((Help) mContext).setResult(0);
	    ((Help) mContext).finish();
	}
    }
}
